<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/generate-parentheses/
      思路：1 https://leetcode-cn.com/problems/generate-parentheses/solution/jsda-gai-shi-zui-jian-ji-de-jie-fa-by-shem/
      2 https://leetcode-cn.com/problems/generate-parentheses/solution/chun-zhao-si-yue-ep09generateparenthesissheng-chen/
    */
    let n = 3;

    function generateParenthesis(n) {
      let res = [];

      let help = (s, left, right) => {
        if (left === n && right === n) res.push(s);
        if (left < n) help(s+'(', left+1, right);
        if (right < left) help(s+')', left, right+1);
      }
      help('', 0, 0);

      return res;
    }
    console.log(generateParenthesis(n));
  </script>
</body>
</html>